package cn.xfdzx.android.apps.shop.activity;

import android.app.Dialog;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.adapter.MyFollow_RetailAdapter;
import cn.xfdzx.android.apps.shop.adapter.MyFollow_XFDAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.MyFollowBean;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.contract.MyFollowContract;
import cn.xfdzx.android.apps.shop.presenter.MyFollowPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.view.SlideRecyclerView;

/*
/* 浏览记录页面
 */
public class MyFollowActivity extends BaseMvpActivity<MyFollowPresenter> implements MyFollowContract.View, View.OnClickListener {
    private ImageView ivBack;      //标题栏-左边按钮
    private SmartRefreshLayout mRefreshLayout;
    private SlideRecyclerView recyclerView;
    private RelativeLayout rl_cancle;//全选布局
    private TextView tv_canclefocus;//取消关注
    private TextView tv_clear;//编辑/完成
    private String limit = "10";
    private int page  = 1;
    private MyFollow_RetailAdapter myFollowRetailAdapter;//零售关注列表
    private List<MyFollowBean.DataBean.RetailBean> retail;
    private MyFollow_XFDAdapter myFollowXfdAdapter;
    private List<MyFollowBean.DataBean.WholesaleBean> wholesale;
    private int pageType;//0-零售1-批发
    private LinearLayout ll_hint;
    private TextView tv_hint;
    private Dialog dialog;//确认清除弹窗



    @Override
    public int getLayoutId() {
        return R.layout.activity_myfollow;
    }

    @Override
    public void initView() {
        pageType = getIntent().getIntExtra("type",0);
        mPresenter = new MyFollowPresenter();
        mPresenter.attachView(this);
        mRefreshLayout = findViewById(R.id.refreshLayout);
        recyclerView = findViewById(R.id.recycle_focus);
        ivBack = findViewById(R.id.iv_back);
        rl_cancle = findViewById(R.id.rl_cancle);
        tv_canclefocus = findViewById(R.id.tv_canclefocus);
        tv_clear = findViewById(R.id.tv_clear);
        ll_hint = findViewById(R.id.ll_hint);
        tv_hint = findViewById(R.id.tv_hint);
        ivBack.setOnClickListener(this);
        tv_clear.setOnClickListener(this);
        tv_canclefocus.setOnClickListener(this);
        initRecycle();
        mPresenter.getFollowList(true,true,limit,page);

        dialog = Utils.addDialog(this, R.layout.dialog, R.string.confirmclearfollow, R.string.confirm, new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Map<String,Object> map = new HashMap<>();
                map.put("type","1");
                mPresenter.getDeleteFollow(map);
                dialog.dismiss();
            }
        });
    }

    private void initRecycle() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        if (pageType == 0){//零售列表
            retail = new ArrayList<>();
            myFollowRetailAdapter = new MyFollow_RetailAdapter(this,retail,mPresenter);
            recyclerView.setAdapter(myFollowRetailAdapter);
            myFollowRetailAdapter.setOnItemClickListener(new ItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    recyclerView.setOpen();
                }
            });
        }else {
            wholesale = new ArrayList<>();
            myFollowXfdAdapter = new MyFollow_XFDAdapter(this,wholesale,mPresenter);
            recyclerView.setAdapter(myFollowXfdAdapter);
            myFollowXfdAdapter.setOnItemClickListener(new ItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    recyclerView.setOpen();
                }
            });
        }

        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

                page = 1;
                mPresenter.getFollowList(true,true,limit,page);

            }
        });


     }

    @Override
    protected void onResume() {
        super.onResume();
//        page = 1;
//        mPresenter.getFollowList(true,true,limit,page);
    }

    @Override
    public void finishRefresh() {
        mRefreshLayout.finishRefresh(true);//刷新完成
    }

    @Override
    public void finishLoadMore() {
        mRefreshLayout.finishLoadMore(true);//加载完成
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(this);
        }
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void getFollowListSuccess(MyFollowBean bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (page == 1) {
                mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        mPresenter.getFollowList(false,true,limit,page);
                    }
                });
            }

            if (pageType == 0) {//零售列表
                if (page == 1){
                    if (EmptyUtil.isNotEmpty(bean.getData())){
                        retail.clear();
                        if (EmptyUtil.isEmpty(bean.getData().getRetail())){
                            ll_hint.setVisibility(View.VISIBLE);
                            tv_hint.setText("您还没有浏览零售商品哦~");
                        }else {
                            for (int i = 0; i < bean.getData().getRetail().size(); i++) {
                                retail.add(new MyFollowBean.DataBean.RetailBean(bean.getData().getRetail().get(i).getTime(),0));
                                bean.getData().getRetail().get(i).setType(1);
                                retail.add(bean.getData().getRetail().get(i));
                            }

                            ll_hint.setVisibility(View.GONE);
                        }
                    }else {
                        retail.clear();
                        ll_hint.setVisibility(View.VISIBLE);
                        tv_hint.setText("您还没有浏览零售商品哦~");
                    }

                }else {
                    if (EmptyUtil.isNotEmpty(bean.getData().getRetail())){
                        for (int i = 0; i < bean.getData().getRetail().size(); i++) {
                            retail.add(new MyFollowBean.DataBean.RetailBean(bean.getData().getRetail().get(i).getTime(),0));
                            bean.getData().getRetail().get(i).setType(1);
                            retail.add(bean.getData().getRetail().get(i));
                        }
                    }


                  //  retail.addAll(bean.getData().getRetail());
                }
                myFollowRetailAdapter.setDate(retail);

                if (EmptyUtil.isEmpty(bean.getData().getRetail())) {
                    mRefreshLayout.finishLoadMoreWithNoMoreData();
                } else {
                    page++;
                }
            }else {
                if (page == 1){
                    if (EmptyUtil.isNotEmpty(bean.getData())){
                        wholesale.clear();
                        if (EmptyUtil.isEmpty(bean.getData().getWholesale())){
                            ll_hint.setVisibility(View.VISIBLE);
                            tv_hint.setText("您还没有浏览批发商品哦~");
                        }else {
                            for (int i = 0; i < bean.getData().getWholesale().size(); i++) {
                                wholesale.add(new MyFollowBean.DataBean.WholesaleBean(bean.getData().getWholesale().get(i).getTime(),0));
                                bean.getData().getWholesale().get(i).setType(1);
                                wholesale.add(bean.getData().getWholesale().get(i));
                            }
                            ll_hint.setVisibility(View.GONE);
                        }
                    }else {
                        wholesale.clear();
                        ll_hint.setVisibility(View.VISIBLE);
                        tv_hint.setText("您还没有浏览批发商品哦~");
                    }

                }else {
                    if (EmptyUtil.isNotEmpty(bean.getData().getWholesale())){
                        for (int i = 0; i < bean.getData().getWholesale().size(); i++) {
                            wholesale.add(new MyFollowBean.DataBean.WholesaleBean(bean.getData().getWholesale().get(i).getTime(),0));
                            bean.getData().getWholesale().get(i).setType(1);
                            wholesale.add(bean.getData().getWholesale().get(i));
                        }
                    }

                   // wholesale.addAll(bean.getData().getWholesale());
                }

                myFollowXfdAdapter.setDate(wholesale);
                if (EmptyUtil.isEmpty(bean.getData().getWholesale())) {
                    mRefreshLayout.finishLoadMoreWithNoMoreData();
                } else {
                    page++;
                }
            }

        }else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void getDeleteFollowSuccess(MyFollowBean bean) {
        recyclerView.closeMenu();
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            page = 1;
            mPresenter.getFollowList(true,true,limit,page);
        }else {
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        if (dialog.isShowing()) {
            dialog.dismiss();

        }
        if (EmptyUtil.isNotEmpty(mPresenter))
        mPresenter.detachView();
        super.onDestroy();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:    //返回按钮点击
                finish();
                break;
            case R.id.tv_clear:    //清空
                if (pageType == 0) {//零售列表)
                    if (EmptyUtil.isNotEmpty(retail)){
                        dialog.show();
                    }else {
                        return;
                    }
                }else {
                    if (EmptyUtil.isNotEmpty(wholesale)){
                        dialog.show();
                    }else {
                        return;
                    }
                }

                break;
        }
    }
}
